package com.buss.FrontController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import jodd.io.http.Http;
import oracle.jdbc.util.Login;

import org.apache.commons.httpclient.HttpRecoverableException;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.dao.jdbc.JdbcDao;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.extend.datasource.DataSourceContextHolder;
import org.jeecgframework.core.extend.datasource.DataSourceType;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.MutiLangUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.UUIDGenerator;
import org.jeecgframework.web.demo.service.test.JeecgJdbcServiceI;
import org.jeecgframework.web.system.pojo.base.TSFunction;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import ch.qos.logback.core.joran.conditional.IfAction;
import ch.qos.logback.core.pattern.color.MagentaCompositeConverter;

import com.buss.article.entity.BnsArticleEntity;
import com.buss.articlegroup.entity.ArticleGroupInfoEntity;
import com.buss.articlegroup.service.ArticleGroupInfoServiceI;
import com.buss.group.entity.BnsGroupArticleEntity;
import com.buss.group.entity.BnsGroupClassEntity;
import com.buss.group.entity.BnsGroupEntity;
import com.buss.group.service.BnsGroupClassServiceI;
import com.buss.group.service.BnsGroupServiceI;
import com.buss.integral.entity.BnsIntegralEntity;
import com.buss.theme.entity.BnsThemeEntity;
import com.buss.user.entity.BnsUsersInfoEntity;



/**   
 * @Title: Controller
 * @Description: 前台群组信息控制层
 * @author lzl
 * @date 2016-09-05
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/groupController")
public class GroupController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(GroupController.class);

	@Autowired
	private BnsGroupClassServiceI groupClassService;
	@Autowired
	private BnsGroupServiceI groupService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private CommonService commonService;
	@Autowired
	private JdbcDao jdbcDao;
	
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 群组分类 群组列表信息
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "groupList")
	public ModelAndView groupList(HttpServletRequest request){
		String groupId = request.getParameter("gid"); 
		String g_sort=request.getParameter("sort");
		Long dataCount=0L;
		int pageParam=0;
		String page=request.getParameter("page");
		int pageSize=20;
		if(StringUtil.isEmpty(page)){
			pageParam=0;
		}else{
			pageParam=Integer.parseInt(page);
		} 
	
		dataCount = systemService.getCountForJdbcParam("select count(1) from bns_group where group_class_id in(select id from bns_group_class where (parent_id=? or id=?))", new Object[]{groupId,groupId});
		  
		String sqlGroup="SELECT id,group_headerimg,group_name,group_memo,group_integral,article_account,member_account FROM bns_group where group_class_id in(select id from bns_group_class where (parent_id=? or id=?))  order by ";
		if(!StringUtil.isEmpty(g_sort)){
			if("zts".equals(g_sort))
				sqlGroup+=" article_account ";
			else if("rs".equals(g_sort))
				sqlGroup+=" member_account ";
			else if("hyd".equals(g_sort)){
				sqlGroup+=" group_integral  ";//积分多少作为活跃度
			}else{//jlsj
				sqlGroup+=" create_date  ";
			}
		}else
			sqlGroup+=" create_date  ";
		
		sqlGroup+=" desc limit ?,?  ";
		List<Map<String, Object>> groupList =systemService.findForJdbc(sqlGroup, new Object[]{groupId,groupId,pageParam,pageSize});
		List<BnsGroupEntity> groupLists=new ArrayList<BnsGroupEntity>();
		for(int i=0;i<groupList.size();i++){
			Map map=groupList.get(i);
			BnsGroupEntity group=new BnsGroupEntity();
			group.setId(map.get("id").toString());//group_headerimg
			group.setGroupHeaderimg(map.get("group_headerimg").toString());
			group.setGroupName(map.get("group_name").toString());
			group.setGroupMemo(map.get("group_memo").toString());
			group.setGroupIntegral(Integer.parseInt(map.get("group_integral").toString()));
			group.setArticleAccount(Integer.parseInt(map.get("article_account").toString()));
			group.setMemberAccount(Integer.parseInt(map.get("member_account").toString()));
			groupLists.add(group);
		}

		int pageCount=0;
		if(dataCount.intValue()>0 && dataCount.intValue()%10==0){
			pageCount=dataCount.intValue()/pageSize;
		} else{
			pageCount=dataCount.intValue()/pageSize+1;
		}
		request.setAttribute("pageCount",pageCount);  
		request.setAttribute("groupList", groupLists); 
		//群组父类
		BnsGroupClassEntity groupClass= (BnsGroupClassEntity) groupService.findByProperty(BnsGroupClassEntity.class, "id", groupId).get(0);
		request.setAttribute("groupClass", groupClass);
		//群组子类 
		List<BnsGroupClassEntity> groupSonClassList=systemService.findByProperty(BnsGroupClassEntity.class, "parentId", groupId);
		request.setAttribute("groupClassList", groupSonClassList);
		if(groupSonClassList==null || groupSonClassList.size()==0){
			request.setAttribute("groupSonClass", groupClass);
			groupClass= (BnsGroupClassEntity) groupService.findByProperty(BnsGroupClassEntity.class, "id", groupClass.getParentId()).get(0);
			request.setAttribute("groupClass", groupClass);
		}else{
			request.setAttribute("groupSonClass", null);
		}
		
		request.setAttribute("groupAccount", dataCount); 
		
		return new ModelAndView("front/groupList");
	}
	

	/**
	 *  群组详细信息
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "groupDetail")
	public ModelAndView groupDetail(HttpServletRequest request){
		String groupId = request.getParameter("sid"); 
		
		BnsGroupEntity groupEntity=(BnsGroupEntity) groupService.findUniqueByProperty(BnsGroupEntity.class, "id", groupId);
		Long dataCount = systemService.getCountForJdbcParam("select count(1) from bns_group where group_integral>(select group_integral from bns_group where id=?)", new Object[]{groupId});
		request.setAttribute("groupDetail", groupEntity);
		request.setAttribute("groupPm", dataCount.intValue()+1);
		
		//导航--父类群组-子类群组
		BnsGroupClassEntity classEntity=(BnsGroupClassEntity)groupClassService.findUniqueByProperty(BnsGroupClassEntity.class, "id", groupEntity.getGroupClassId());
		request.setAttribute("sonClass",classEntity);
		classEntity=(BnsGroupClassEntity)groupClassService.findUniqueByProperty(BnsGroupClassEntity.class, "id", classEntity.getParentId());
		request.setAttribute("parentClass",classEntity);
		//群组成员--新-活跃
		List<Map<String,Object>> groupUsers=systemService.findForJdbc("SELECT G.user_id,U.user_name from bns_group_user G,bns_users_info U where U.user_id=G.user_id and G.group_id=? order by U.reg_time desc limit 9 ", new Object[]{groupId});
		request.setAttribute("groupUserList", groupEntity);
		//
		
		return new ModelAndView("front/groupDetail");
	}
	

	/**
	 *  我的群组
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "mygroup")
	public ModelAndView mygroup(HttpServletRequest request){ 
		HttpSession session = request.getSession();
		Object mbIdV = session.getAttribute("mbId");
		if (!StringUtil.isNotEmpty(mbIdV)) {
			return new ModelAndView("front/login");
		}
		 
		String mode=request.getParameter("mode");
		int page=0,pageSize=20;
		
		String sqlGroup="select g.id,g.group_name,g.group_headerimg from bns_group_class c,bns_group g,bns_group_user u where c.id=g.group_class_id and g.id=u.group_id and u.user_id=? ";
		String sqlGroupClass="";
		if("mythread".equals(mode)){//我的话题
			String typeid=request.getParameter("typeid");
			String param_p=mbIdV.toString();
			//显示最近的5条话题 
			sqlGroupClass="select a.id,a.title,a.reply_account,a.view_account,a.last_publish_userid,a.last_publish_time,u.user_name from bns_group_article a,bns_users_info u where u.user_id=a.last_publish_userid ";
			if(StringUtil.isEmpty(typeid)){
				sqlGroupClass+=" and a.group_id in(select group_id from bns_group_user where user_id=?) ";
				param_p=mbIdV.toString();
			}else{
				sqlGroupClass+=" and a.group_id=? ";
				param_p=typeid;
			}
			sqlGroupClass+=" order by a.create_date desc limit 5 "; 
			List<Map<String,Object>> articles=systemService.findForJdbc(sqlGroupClass, new Object[]{param_p});
			request.setAttribute("articles", articles); 
			
			List<Map<String,Object>> groupClassName=systemService.findForJdbc(sqlGroup, new Object[]{mbIdV});
			request.setAttribute("groupClass", groupClassName);
			
		}else if("join".equals(mode)){//我的参与--即我加入的群组
			List<Map<String,Object>> groups=systemService.findForJdbc("select g.id,g.group_name,g.group_headerimg from bns_group g,bns_group_user u where g.id=u.group_id and u.user_id=? order by g.create_date desc limit 50 ", new Object[]{mbIdV});
			request.setAttribute("myjoingroups",groups);
			
		}else if("manager".equals(mode)){//我管理的 
			List<Map<String,Object>> groups=systemService.findForJdbc("select g.id,g.group_name,g.group_headerimg from bns_group g,bns_group_user u where g.id=u.group_id and u.user_type=2 and u.user_id=? order by g.create_date desc limit 50 ", new Object[]{mbIdV});
			request.setAttribute("mymagroups",groups);
			
		}else{//群组话题
			String typeid=request.getParameter("typeid");
			String param_p=mbIdV.toString();
			//显示最近的5条话题 
			sqlGroupClass="select a.id,a.title,a.reply_account,a.view_account,a.last_publish_userid,a.last_publish_time,u.user_name from bns_group_article a,bns_users_info u where u.user_id=a.last_publish_userid ";
			if(StringUtil.isEmpty(typeid)){
				sqlGroupClass+=" and a.group_id in(select group_id from bns_group_user where user_id=?) ";
				param_p=mbIdV.toString();
			}else{
				sqlGroupClass+=" and a.group_id=? ";
				param_p=typeid;
			}
			sqlGroupClass+=" order by a.create_date desc limit 5 "; 
			List<Map<String,Object>> articles=systemService.findForJdbc(sqlGroupClass, new Object[]{param_p});
			request.setAttribute("articles", articles); 
			
			//新话题-最近30天--我参与的群组，最近30天内发表过的话题列表
			sqlGroupClass="select a.id,a.title,a.reply_account,a.view_account,a.last_publish_userid,a.last_publish_time,u.user_name from bns_group_article a,bns_users_info u where u.user_id=a.last_publish_userid ";
			if(StringUtil.isEmpty(typeid)){
				sqlGroupClass+=" and a.group_id in(select group_id from bns_group_user where user_id=?) ";
				param_p=mbIdV.toString();
			}else{
				sqlGroupClass+=" and a.group_id=? ";
				param_p=typeid;
			}
			sqlGroupClass+=" and TO_DAYS(NOW()) - TO_DAYS(a.create_date) <= 30 order by a.create_date desc limit 10 "; 
			List<Map<String,Object>> articles2=systemService.findForJdbc(sqlGroupClass, new Object[]{param_p});
			request.setAttribute("articles30day", articles2); 
			
			List<Map<String,Object>> groupClassName=systemService.findForJdbc(sqlGroup, new Object[]{mbIdV});
			request.setAttribute("groupClass", groupClassName);
			
		}
		  
		return new ModelAndView("front/group_my");
	}

	@RequestMapping(params = "groupDemo")
	public ModelAndView groupDemo(HttpServletRequest request){
		return new ModelAndView("front/thread_replay");
	}
	/**
	 *  我的群组
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "thread")
	public ModelAndView thread(HttpServletRequest request){ 
		HttpSession session = request.getSession();
		Object mbIdV = session.getAttribute("mbId");
		if (!StringUtil.isNotEmpty(mbIdV)) {
			return new ModelAndView("front/login");
		}
		String ard=request.getParameter("ard");//文章ID
		String pageIndex=request.getParameter("page");
		String authorid=request.getParameter("authorid");
		
		int page=0,pageSize=10;
		if(!StringUtil.isNumeric(pageIndex))
			page=0;
		else
			page=Integer.parseInt(pageIndex);
		if(page>0)
			page=page-1;
		//返回文章信息-发布人信息
		//获取文章+评论列表
		String sql="SELECT a.id,a.create_by,a.create_date,a.title,a.content,a.is_new,a.is_up,a.is_close,a.is_zambia,";
		sql+="a.view_account,a.reply_account,u.integral,u.release_num,u.comments_num,a.update_date,";
		sql+="a.is_recommend,a.article_type,a.parent_id,u.user_id,u.user_name,u.qq,u.wechat,u.email,u.personal_homepage,u.prestige,";
		sql+="u.charm,u.contrbute,u.lunbi,u.jindou,u.cash from bns_group_article a left join bns_users_info u on u.user_id=a.create_userid ";
		sql+=" where (a.id=? or a.parent_id=?) ";
		if(!StringUtil.isEmpty(authorid)){
			sql+=" and u.user_id='"+authorid+"' ";
		}
		sql+=" order by a.create_date asc limit ?,? ";
		List<Map<String,Object>> articles=systemService.findForJdbc(sql, new Object[]{ard,ard,page*pageSize,pageSize}); 
		request.setAttribute("articles", articles);
		//获取文章标题内容
		BnsGroupArticleEntity articleEntity=groupClassService.getEntity(BnsGroupArticleEntity.class, ard);
		request.setAttribute("article", articleEntity);
		
		String sqlCount="select count(1) from bns_group_article where (id=? or parent_id=?)";
		if(!StringUtil.isEmpty(authorid)){
			sqlCount+=" and create_by='"+authorid+"' ";
		}
		Long total=systemService.getCountForJdbcParam(sqlCount, new Object[]{ard,ard}); 
		int pageCount = 0;
		if (total.intValue() > 0 && total.intValue() % pageSize == 0) {
			pageCount = total.intValue() / pageSize;
		} else {
			pageCount = total.intValue() / pageSize + 1;
		}
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("page", page);
		  
		//获取群组id,NAME,群组小类、大类ID,NAME
		sql="SELECT g.id,g.group_id,s.group_name,b.id as group_son_id,b.group_name as group_son_name,b.parent_id as group_parent_id,";
		sql+= "b2.group_name as group_parent_name from bns_group_article g ";
		sql+="left join bns_group_class b on b.id=g.group_son_id ";
		sql+="left join bns_group s on s.id=g.group_id ";
		sql+="left join bns_group_class b2 on b2.id=b.parent_id ";
		sql+="where g.id=? ";
		List<Map<String,Object>> groupSonGroup=systemService.findForJdbc(sql, new Object[]{ard});
		if(groupSonGroup!=null && groupSonGroup.size()>0)
			request.setAttribute("groupLink", groupSonGroup.get(0));
		else
			request.setAttribute("groupLink", null);
		//评分模块列表数据
		//评分记录列表
		List<Map<String, Object>> pfList=systemService.findForJdbc("select s.id,s.article_id,s.experience,s.prestige,s.charm,s.contribution,s.jindou,s.reason,u.user_id,u.headerimg,u.user_name from bns_group_article_score s,bns_users_info u where u.user_id=s.score_userid and s.article_id in(select a.id from bns_group_article a where a.id=? or a.parent_id=?) order by s.create_date desc ", new Object[]{ard,ard});
		request.setAttribute("pflist", pfList);
		
		return new ModelAndView("front/thread");
	}

	/**
	 * 举报
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "replayJuBao")
	@ResponseBody
	public AjaxJson replayJuBao(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		HttpSession session = req.getSession();
		Object mbIdV = session.getAttribute("mbId");
		if (!StringUtil.isNotEmpty(mbIdV)) {
			j.setSuccess(false);
			j.setMsg("未登录或者登录超时，请重新登录！");
			return j;
		}
		String art_id =StringUtil.getEncodePra(req.getParameter("ard"));
		if(StringUtil.isEmpty(art_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		} 
		String articleId =StringUtil.getEncodePra(req.getParameter("articleid"));
		if(StringUtil.isEmpty(articleId)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		} 
		String reason =StringUtil.getEncodePra(req.getParameter("reason"));
		if(StringUtil.isEmpty(reason)){
			j.setSuccess(false);
			j.setMsg("举报理由不能为空！");
			return j;
		} 
		String receive_id=StringUtil.getEncodePra(req.getParameter("recv_id"));
		if(StringUtil.isEmpty(receive_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		if(!StringUtil.isNumeric(receive_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}//comment_id

		String userName=session.getAttribute("userName").toString();
		String title=req.getParameter("title"); 
		//先判断是否举报过一次了。不能多次举报
		Long count=systemService.getCountForJdbcParam("SELECT COUNT(1) FROM bns_article_jubao where article_type=2 and article_id=? and comment_id=? and user_id=? ", new Object[]{art_id,articleId,mbIdV.toString()});
		if(count.intValue()>0){
			j.setSuccess(false);
			j.setMsg("您已经举报此评论，不需要在举报操作！");
			return j;
		}
		String[] sqlArr=new String[2];
		//1.插入一条举报记录信息
		//2.新增一条系统通知记录
		//3.扣除会员个人对应的积分信息
		sqlArr[0]="insert into bns_article_jubao(id,create_name,create_by,create_date,article_type,article_id,user_id,";
		sqlArr[0]+="jubao_type,article_title,comment_id,status) values('"+UUIDGenerator.generate()+"','"+userName+"',";
		sqlArr[0]+="'"+mbIdV.toString()+"',sysdate(),2,'"+art_id+"','"+mbIdV.toString()+"','"+reason+"','"+title+"',";
		sqlArr[0]+="'"+articleId+"',0) ";
		//判断是否为0的
		sqlArr[1]="insert into bns_message(id,content,message_types,create_date,create_name,create_by,";//art_id主题ID
		sqlArr[1]+="receive_id,states,is_read) values('"+UUIDGenerator.generate()+"','您的主题："+title+" 的帖子被  <a href=\\'dd\\'>"+userName+"</a> 举报了，举报理由是： "+reason+"',";
		sqlArr[1]+="3,sysdate(),'"+userName+"','"+mbIdV.toString()+"','"+receive_id+"',0,0)";
		 
		int n=jdbcDao.batchUpdateSqlArr(sqlArr); 
		if(n>0){
			//评分记录列表 
			j.setSuccess(true);
			j.setMsg("举报操作完成！");
			//不需要，只要前台HTML+1即可
		}else {
			j.setSuccess(false);
			j.setMsg("操作失败，请重试！");
		}
		return j;
	}
	
	/**
	 * 评分
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "replayPinfen")
	@ResponseBody
	public AjaxJson replayPinfen(HttpServletRequest req){
		AjaxJson j = new AjaxJson();
		HttpSession session = req.getSession();
		Object mbIdV = session.getAttribute("mbId");
		if (!StringUtil.isNotEmpty(mbIdV)) {
			j.setSuccess(false);
			j.setMsg("未登录或者登录超时，请重新登录！");
			return j;
		}
		String art_id =StringUtil.getEncodePra(req.getParameter("ard"));
		if(StringUtil.isEmpty(art_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		} 
		String articleId =StringUtil.getEncodePra(req.getParameter("articleid"));
		if(StringUtil.isEmpty(articleId)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		} 
		String receive_id=StringUtil.getEncodePra(req.getParameter("recv_id"));
		if(StringUtil.isEmpty(receive_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		if(!StringUtil.isNumeric(receive_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		String ACTION_MEMO="";
		String experience=req.getParameter("score1");//经验
		if(!StringUtil.isNumeric(experience)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}else{
			ACTION_MEMO=" 经验+"+experience;
		}
		String prestige=req.getParameter("score2");//威望
		if(!StringUtil.isNumeric(prestige)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}else{
			ACTION_MEMO=" 威望+"+prestige;
		}
		String charm=req.getParameter("score3");//魅力
		if(!StringUtil.isNumeric(charm)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}else{
			ACTION_MEMO=" 魅力+"+charm;
		}
		String contrbute=req.getParameter("score4");//贡献
		if(!StringUtil.isNumeric(contrbute)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}else{
			ACTION_MEMO=" 贡献+"+contrbute;
		}
		String jindou=req.getParameter("score5");//金豆
		if(!StringUtil.isNumeric(jindou)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}else{
			ACTION_MEMO=" 金豆+"+jindou;
		}
		
		String reason=req.getParameter("reason");
		if(StringUtil.isEmpty(reason)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		
		String userName=session.getAttribute("userName").toString();
		String title=req.getParameter("title"); 
		//判断会员个人积分是否足够扣
		BnsUsersInfoEntity userInfo=(BnsUsersInfoEntity) systemService.findByProperty(BnsUsersInfoEntity.class, "userId", Integer.parseInt(mbIdV.toString())).get(0);
		int u_jy=userInfo.getExperience();
		int u_ww=userInfo.getPrestige();
		int u_ml=userInfo.getCharm();
		int u_gx=userInfo.getContrbute();
		int u_jd=userInfo.getJindou();
		if((u_jy-Integer.parseInt(experience))<0){
			j.setSuccess(false);
			j.setMsg("您的经验不足，不能对经验进行评分！");
			return j;
		}
		if((u_ww-Integer.parseInt(prestige))<0){
			j.setSuccess(false);
			j.setMsg("您的威望不足，不能对威望进行评分！");
			return j;
		}
		if((u_ml-Integer.parseInt(charm))<0){
			j.setSuccess(false);
			j.setMsg("您的魅力不足，不能对魅力进行评分！");
			return j;
		}
		if((u_gx-Integer.parseInt(contrbute))<0){
			j.setSuccess(false);
			j.setMsg("您的贡献不足，不能对贡献进行评分！");
			return j;
		}
		if((u_jd-Integer.parseInt(jindou))<0){
			j.setSuccess(false);
			j.setMsg("您的金豆不足，不能对金豆进行评分！");
			return j;
		} 

		String[] sqlArr=new String[3];
		//1.插入一条评分记录信息
		//2.新增一条系统通知记录
		//3.扣除会员个人对应的积分信息
		sqlArr[0]="insert into bns_group_article_score(id,create_name,create_by,create_date,article_id,score_userid,";
		sqlArr[0]+="experience,prestige,charm,contribution,jindou,reason) values('"+UUIDGenerator.generate()+"','"+userName+"',";
		sqlArr[0]+="'"+mbIdV.toString()+"',sysdate(),'"+articleId+"','"+mbIdV.toString()+"','"+experience+"','"+prestige+"',";
		sqlArr[0]+="'"+charm+"','"+contrbute+"','"+jindou+"','"+reason+"') ";
		//判断是否为0的
		sqlArr[1]="insert into bns_message(id,content,message_types,create_date,create_name,create_by,";//art_id主题ID
		sqlArr[1]+="receive_id,states,is_read) values('"+UUIDGenerator.generate()+"','您的主题："+title+" 的帖子被  <a href=\\'dd\\'>"+userName+" 评分 </a> "+ACTION_MEMO+"',";
		sqlArr[1]+="3,sysdate(),'"+userName+"','"+mbIdV.toString()+"','"+receive_id+"',0,0)";
		
		sqlArr[2]="update bns_users_info set experience=experience-"+experience+",prestige=prestige-"+prestige+",";
		sqlArr[2]+="charm=charm-"+charm+",contrbute=contrbute-"+contrbute+",jindou=jindou-"+jindou+" where user_id='"+mbIdV.toString()+"' ";

		int n=jdbcDao.batchUpdateSqlArr(sqlArr); 
		if(n>0){
			//评分记录列表
			List<Map<String, Object>> pfList=systemService.findForJdbc("select s.id,s.experience,s.prestige,s.charm,s.contribution,s.jindou,s.reason,u.user_id,u.headerimg,u.user_name from bns_group_article_score s,bns_users_info u where u.user_id=s.score_userid and s.article_id=? order by s.create_date desc ", new Object[]{articleId});
			j.setObj(pfList);
			j.setSuccess(true);
			//不需要，只要前台HTML+1即可
		}else {
			j.setSuccess(false);
		}
		return j;
	}

	/**
	 * 获取更多评分记录
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "getPinfenList")
	@ResponseBody
	public AjaxJson getPinfenList(HttpServletRequest req){ 
		AjaxJson j = new AjaxJson();
		String articleId =StringUtil.getEncodePra(req.getParameter("articleId"));
		if(StringUtil.isEmpty(articleId)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		} 
		//评分记录列表
		List<Map<String, Object>> pfList=systemService.findForJdbc("select s.id,s.experience,s.prestige,s.charm,s.contribution,s.jindou,s.reason,u.user_id,u.headerimg,u.user_name from bns_group_article_score s,bns_users_info u where u.user_id=s.score_userid and s.article_id=? order by s.create_date desc ", new Object[]{articleId});
		j.setObj(pfList); 
		return j;
	} 
	
	/**
	 * 文章评论支持或者反对执行事件
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "replayAction")
	@ResponseBody
	public AjaxJson replayAction(HttpServletRequest req){
		AjaxJson j = new AjaxJson();//StringUtil.getEncodePra(req.getParameter("id"));
		HttpSession session = req.getSession();
		Object mbIdV = session.getAttribute("mbId");
		if (!StringUtil.isNotEmpty(mbIdV)) {
			j.setSuccess(false);
			j.setMsg("未登录或者登录超时，请重新登录！");
			return j;
		}
		String article_id =req.getParameter("ard");//文章ID 
		if(StringUtil.isEmpty(article_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		String action=req.getParameter("action");
		if(StringUtil.isEmpty(action)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}  
		String receive_id=req.getParameter("recv_id");
		if(StringUtil.isEmpty(receive_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		if(!StringUtil.isNumeric(receive_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		//1.更新文章记录表对应的字段
		//2.插入一条对应的记录
		//3.通知，赞反对，，了一下
		//4.如果是收藏的话，往收藏表里面添加一条收藏信息

		//先判断是否已支持或者反对，收藏过了
		boolean isAction=false;
		 
		int LEN=3;
		if("keep".equals(action))
			LEN=4;
		
		String[] sqlArr=new String[LEN];
		String ACTION_TYPE_MEMO="";
		String sql="update bns_group_article set ";
		if("support".equals(action) || "against".equals(action)){
			Long count=systemService.getCountForJdbcParam("select count(1) from bns_article_action_record where user_id=? and action=1 and (action_type=? or action_type=?)",new Object[]{mbIdV.toString(),"support","against"});
			if(count.intValue()==1)
				isAction=true;
		}
		if("support".equals(action)){
			sql+=" support_account=support_account+1 ";
			ACTION_TYPE_MEMO=" 支持了一下 ";
		}else if("against".equals(action)){
			sql+=" oppose_account=oppose_account+1 ";
			ACTION_TYPE_MEMO=" 反对了一下 ";
		}else if("keep".equals(action)){//收藏数
			sql+=" keep_account=keep_account+1 ";
			Long count=systemService.getCountForJdbcParam("select count(1) from bns_article_action_record where user_id=? and action=1 and action_type=?)",new Object[]{mbIdV.toString(),"keep"});
			if(count.intValue()==1)
				isAction=true;
			
			ACTION_TYPE_MEMO=" 收藏了 ";
		}else if("share".equals(action)){//分享
			sql+=" share_account=share_account+1 ";
			ACTION_TYPE_MEMO=" 分享了 ";
		}else {
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		String userName=session.getAttribute("userName").toString();
		String title=req.getParameter("title"); 
		
		if(isAction){
			j.setSuccess(false);
			j.setMsg("您已经对此回帖投票过了！");
			return j;
		}
		sqlArr[0]=sql+" where id='"+article_id+"' ";
		sqlArr[1]="insert into bns_article_action_record(id,create_name,create_by,create_date,article_type,";
		sqlArr[1]+="article_id,user_id,action,action_type) values('"+UUIDGenerator.generate()+"','"+userName+"','"+mbIdV.toString()+"',";
		sqlArr[1]+="sysdate(),2,'"+article_id+"','"+mbIdV.toString()+"',1,'"+action+"')";
		sqlArr[2]="insert into bns_message(id,content,message_types,create_date,create_name,create_by,";
		sqlArr[2]+="receive_id,states,is_read) values('"+UUIDGenerator.generate()+"','您的主题："+title+"的帖子被  <a href=\\'dd\\'>"+userName+" "+ACTION_TYPE_MEMO+"</a>',";
		sqlArr[2]+="3,sysdate(),'"+userName+"','"+mbIdV.toString()+"','"+receive_id+"',0,0)";
		if("keep".equals(action)){
			sqlArr[3]="insert into bns_collects(id,collect_user,bycollect_id,collect_url,bycollect_name,collect_type,";//收藏表
			sqlArr[3]+="collect_time,collect_username) values('"+UUIDGenerator.generate()+"','"+mbIdV.toString()+"',";
			sqlArr[3]+="'"+article_id+"','groupController.do?thread&ard="+article_id+"','"+title+"',1,sysdate(),'"+userName+"')";
		}   
		//判断是否分享过一次了 
		if("share".equals(action)){
			//分享过的话，不在保存一次分享记录，反之保存
			//Long count=systemService.getCountForJdbcParam("select count(1) from bns_article_action_record where user_id=? and action=1 and action_type=?)",new Object[]{mbIdV.toString(),"keep"});
			//if(count.intValue()==1)
			//	isAction=true;
			//待完善
		}
		int n=jdbcDao.batchUpdateSqlArr(sqlArr); 
		if(n>0){
			j.setSuccess(true);
			//返回数量
			//不需要，只要前台HTML+1即可
		}else {
			j.setSuccess(false);
			j.setMsg("评分失败！");
		}
		return j;
	}
	//论坛文章回复评论
	/**
	 * 回复
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "replayComment")
	@ResponseBody
	public AjaxJson replayComment(HttpServletRequest req) {
		AjaxJson j = new AjaxJson();//StringUtil.getEncodePra(req.getParameter("id"));
		HttpSession session = req.getSession();
		Object mbIdV = session.getAttribute("mbId");
		if (!StringUtil.isNotEmpty(mbIdV)) {
			j.setSuccess(false);
			j.setMsg("未登录或者登录超时，请重新登录！");
			return j;
		}
		String article_id =req.getParameter("ard");//文章ID 
		if(StringUtil.isEmpty(article_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		String text=req.getParameter("text");
		if(StringUtil.isEmpty(text)){
			j.setSuccess(false);
			j.setMsg("请输入评论内容！");
			return j;
		} 
		String receive_id=req.getParameter("recv_id");
		if(StringUtil.isEmpty(receive_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		if(!StringUtil.isNumeric(receive_id)){
			j.setSuccess(false);
			j.setMsg("参数错误！");
			return j;
		}
		//加载积分规则
		List<Map<String,Object>> jfRuleList=systemService.findForJdbc("SELECT action,period_num,jy,ww,yb,ml,gx,gxun,jd,xj from bns_integral_rule where (action='评论' or action='被评论')", new Object[]{});
		Map recommentMap=new HashMap();
		Map bRecommentMap=new HashMap();
		for(Map map : jfRuleList){
			if("评论".equals(map.get("action"	).toString()))
				recommentMap=map;
			else {
				bRecommentMap=map;
			}
		}
		
		String group_son_id =req.getParameter("group_sid");//群组子类ID
		String group_id =req.getParameter("gid");
		String userName=session.getAttribute("userName").toString();
		List<String> strList=new ArrayList<String>();
		String USER_ID=mbIdV.toString();
		//1.添加评论到文章表中
		String auuid=UUIDGenerator.generate();
		//更新回复数量+最后回复信息
		strList.add("update bns_group_article set last_publish_userid='"+USER_ID+"',last_publish_time=sysdate() ,reply_account=reply_account+1 where id='"+article_id+"' ");//article_type
		//插入评论信息
		strList.add("insert into bns_group_article(id,create_name,create_by,create_date,content,group_son_id,group_id,create_userid,create_time,article_type,parent_id) values('"+auuid+"','"+userName+"','"+USER_ID+"',sysdate(),'"+text+"','"+group_son_id+"','"+group_id+"','"+USER_ID+"',sysdate(),1,'"+article_id+"')");
		//获取设置的积分规则
		//判断今天总次数
		//被评论--文章发布人ID
		BnsGroupArticleEntity article=systemService.findUniqueByProperty(BnsGroupArticleEntity.class, "id", article_id);
		int BReplayCommentUserId=article.getCreateUserid();
		//如果是同一个人，那么被评论人不加
		if(BReplayCommentUserId==Integer.parseInt(USER_ID)){}else{
			if(!bRecommentMap.isEmpty())//更新被评论人积分规则信息
				strList.add("update bns_users_info set comments_num=comments_num+1,prestige=prestige +"+bRecommentMap.get("ww").toString()+",charm=charm+"+bRecommentMap.get("ml").toString()+",contrbute=contrbute+"+bRecommentMap.get("gx").toString()+",lunbi=lunbi+"+bRecommentMap.get("yb").toString()+",jindou=jindou+"+bRecommentMap.get("jd").toString()+",cash=cash+"+bRecommentMap.get("xj").toString()+",experience=experience+"+bRecommentMap.get("jy").toString()+" where user_id='"+USER_ID+"' ");//发表评论获得积分
		}
		//评论
		if(!recommentMap.isEmpty()){//发布评论人积分规则信息
			strList.add("update bns_users_info set comments_num=comments_num+1,prestige=prestige +"+recommentMap.get("ww").toString()+",charm=charm+"+recommentMap.get("ml").toString()+",contrbute=contrbute+"+recommentMap.get("gx").toString()+",lunbi=lunbi+"+recommentMap.get("yb").toString()+",jindou=jindou+"+recommentMap.get("jd").toString()+",cash=cash+"+recommentMap.get("xj").toString()+",experience=experience+"+recommentMap.get("jy").toString()+" where user_id='"+USER_ID+"' ");//发表评论获得积分
		}  
		//通知 
		strList.add("insert into bns_message(id,content,message_types,create_date,create_name,create_by,receive_id,states,is_read) values('"+UUIDGenerator.generate()+"','您的主题："+article.getTitle()+"的帖子被  <a href=\\'dd\\'>"+userName+"评论了</a>',3,sysdate(),'"+userName+"','"+mbIdV.toString()+"','"+receive_id+"',0,0)");
		
		String[] sqlArr=new String[strList.size()];
		for(int i=0;i<strList.size();i++){
			sqlArr[i]=strList.get(i);
		}
		int n=jdbcDao.batchUpdateSqlArr(sqlArr);
		Map ret_map=new HashMap();
		if(n>0){
			j.setSuccess(true);
			//返回评论信息-当前会员个人信息
			String sql="SELECT a.id,a.create_by,a.create_date,a.title,a.content,a.is_new,a.is_up,a.is_close,a.is_zambia,";
			sql+="a.view_account,a.reply_account,u.integral,u.release_num,u.comments_num,";
			sql+="(select count(1)+1 from bns_group_article b where b.parent_id=a.parent_id) as cengshu,";
			sql+="a.is_recommend,a.article_type,a.parent_id,u.user_id,u.user_name,u.qq,u.wechat,u.email,u.personal_homepage,u.prestige,";
			sql+="u.charm,u.contrbute,u.lunbi,u.jindou,u.cash from bns_group_article a left join bns_users_info u on u.user_id=a.create_userid ";
			sql+=" where a.id=? and u.user_id=? ";
			List<Map<String,Object>> articles=systemService.findForJdbc(sql, new Object[]{auuid,USER_ID}); 
			j.setObj(articles.get(0));
			
		}else{
			j.setSuccess(false);
			j.setMsg("出错了！");
		}
		return j;
	}
		
}
